<?php


namespace App\Repositories;


use App\Models\Entryorder as Model;
use App\Models\EntryorderProduct;
use Exception;
use LTools\Repositories\RepositoryAbstract;

class Entryorder extends RepositoryAbstract
{
    /**
     * Entryorder constructor.
     * @param Model $model
     * @author luffyzhao@vip.126.com
     */
    public function __construct(Model $model)
    {
        $this->model = $model;
    }

    /**
     * @param array $attributes
     * @return Model|bool|\Illuminate\Database\Eloquent\Model
     * @author luffyzhao@vip.126.com
     */
    public function create(array $attributes = [])
    {
        /** @var Model $model */
        $model = parent::create($attributes);
        // 添加产品
        $model->products()->createMany(
            $this->handleRequestProducts($attributes['products'])
        );

        $model->logs()->create(['remark' => '添加成功']);
        return $model;
    }

    /**
     * @param $id
     * @param array $values
     * @return bool|\Illuminate\Database\Eloquent\Model
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function update($id, array $values)
    {
        /** @var Model $model */
        $model = $this->lock($id);
        $model->fill($values)->saveOrFail();
        // 添加产品
        $model->products()->delete();
        $model->products()->createMany(
            $this->handleRequestProducts($values['products'])
        );
        $model->logs()->create(['remark' => '更新成功']);
        return $model;
    }

    /**
     * @param $id
     * @return Model
     * @author luffyzhao@vip.126.com
     */
    public function edit($id)
    {
        /** @var Model $model */
        $model = $this->find($id);
        $model->loadMissing(['products.product']);
        return $model;
    }

    /**
     * @param $id
     * @return Model
     * @throws Exception
     * @throws \Throwable
     * @author luffyzhao@vip.126.com
     */
    public function affirm($id)
    {
        /** @var Model $model */
        $model = $this->lock($id);

        if($model->isDraft()){

            $model->products->each(function (EntryorderProduct $item){
                if(!$item->product->isUnlock()){
                    throw new Exception(sprintf('产品[%s]已锁定，或者产品还是草稿状态！', $item->product->getAttribute('sku')));
                }
            });

            $model->setAttribute('status', 1);
            $model->saveOrFail();

            $model->logs()->create(['remark' => '确认成功']);

            return $model;
        }
        throw new Exception("入库单不是草稿状态，不能确认！");
    }


    /**
     * @param $products
     * @return \Generator
     * @author luffyzhao@vip.126.com
     */
    protected function handleRequestProducts(array $products)
    {
        foreach ($products as $product) {
            yield  $this->yieldProduct($product);
        }
    }

    /**
     * @param $product
     * @return array
     * @author luffyzhao@vip.126.com
     */
    protected function yieldProduct(array $product)
    {
        $product['product_id'] = \App\Models\Product::query()->where('sku', '=', $product['sku'])->value('id');
        return $product;
    }
}
